package com.ews.onlineexchange.service.schedule;

import java.util.Calendar;
import java.util.Date;

import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.hibernate.CacheMode;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;

import com.ews.onlineexchange.enumType.RecordStatus;
import com.ews.onlineexchange.model.DeterminedRecord;
import com.ews.onlineexchange.service.NotificationService;
import com.ews.onlineexchange.service.SmsService;

@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DeterminedRecordTipTimer {

	
	@Inject
	EntityManager entityManager;
	@Inject
	NotificationService notificationService;
	
	private final static String QUERY_ALL_RECORD = "" 
				+ " SELECT record"
				+ " FROM DeterminedRecord record"
				+ " WHERE record.delFlag = 0 "
				+ " AND record.hasTipReporter = 0 "
				+ " AND (record.auditted IS NULL OR record.auditted = 0 ) AND record.recordStatus != 'Deleted' ";

	@Schedule(hour="8", minute="45", second="0", info = "certWarnTimer", persistent = false)
	public void check() {
		
		Session session = entityManager.unwrap( Session.class );	
		ScrollableResults sr = session
	        .createQuery( QUERY_ALL_RECORD )
	        .setCacheMode( CacheMode.IGNORE )
	        .scroll( ScrollMode.FORWARD_ONLY );	
		long count = 0l;
		while (sr.next()) {
			DeterminedRecord record = (DeterminedRecord) sr.get(0);
			Calendar endCal = Calendar.getInstance();
			endCal.setTime(record.getEndDate());
			//截止日期的前五天之后才可编辑
			endCal.set(Calendar.DAY_OF_MONTH, endCal.get(Calendar.DAY_OF_MONTH) - 5);
			Date minus5DaysDate = endCal.getTime();
			Date now = new Date();
			if(now.after(minus5DaysDate) && now.before(record.getEndDate())) {
				notificationService.sendHandleNotification(record);
				record.setHasTipReporter(true);
				session.update(record);
				count++;
				if (count % 10 == 0) {
					session.flush();
					session.clear();
				}
				session.flush();	
			}
		}
	}
	
	@Inject
	SmsService smsService;
	
	private final static String QUERY_NEED_INVALIDATE_RECORD_EQL = "" 
				+ " SELECT 		record"
				+ " FROM 		DeterminedRecord record"
				+ " WHERE 		record.delFlag = 0 "
				+ " AND 		record.hasTipReporter = 0 "
				+ " AND 	(	record.auditted IS NULL OR record.auditted = 0 ) "
				+ "	AND 		record.recordStatus != 'Deleted' "
				+ "	AND			record.limitDate < CURRENT_DATE "
				+ "	AND			record.limitDate IS NOT NULL " ;

	@Schedule( hour = "9", minute = "0", second = "0", info = "inValidateDeterminedRecordTimer", persistent = false )
	public void inValidate() {
		Session session = entityManager.unwrap( Session.class );	
		ScrollableResults sr = session
	        .createQuery( QUERY_NEED_INVALIDATE_RECORD_EQL )
	        .setCacheMode( CacheMode.IGNORE )
	        .scroll( ScrollMode.FORWARD_ONLY );	
		long count = 0l;
		while (sr.next()) {
			DeterminedRecord record = (DeterminedRecord) sr.get(0);
			/*
			 * 自动禁用记录，并自动审核
			 */
			record.setAuditted(true);
			record.setAuditDate(new Date());
			record.setRecordStatus(RecordStatus.Deleted);
			session.update(record);
			//短信通知功能
			smsService.determinedRecordFailNotification(record.getSupplier());
			count++;
			if (count % 10 == 0) {
				session.flush();
				session.clear();
			}
			session.flush();	
//			String legalPersonPhoneNumber = record.getSupplier().getLegalPersonPhoneNumber();
			
		}
	}
}
